##########################################################################################

library(data.table)
library(optparse)

##########################################################################################

option_list <- list(
    make_option(c("--info_sample_file"), type = "character"),
    make_option(c("--info_patient_file"), type = "character"),
    make_option(c("--out_path"), type = "character")
)

if(1!=1){
    
    work_dir <- "~/20220915_gastric_multiple/dna_combinePublic/"
    maf_cancer_file <- paste(work_dir,"/maf_public/All_use.maf",sep="")
    maf_im_file <- paste(work_dir,"/maf_public/All_use.IM.maf",sep="")
	images_path <- paste(work_dir,"/images/mutRate",sep="")
	info_file <- paste(work_dir,"/public_ref/combine/MutationInfo.combine.tsv",sep="")

}

###########################################################################################

parseobj <- OptionParser(option_list=option_list, usage = "usage: Rscript %prog [options]")
opt <- parse_args(parseobj)
print(opt)

info_sample_file <- opt$info_sample_file
info_patient_file <- opt$info_patient_file
out_path <- opt$out_path

dir.create(out_path , recursive = T)

###########################################################################################

info_sample <- data.frame(fread(info_sample_file))
info_patient <- data.frame(fread( info_patient_file ))

info_patient_use <- subset(info_patient,info_patient$From=="NJMU" & info_patient$Molecular.subtype =="CIN" & info_patient$Class !="IGC + DGC" )
colnames(info_patient_use)[1] <- "ID"


info_sample_use <- subset(info_sample,info_sample$ID %in% info_patient_use$ID & info_sample$Class!="IM")
info_all <- merge(info_sample_use,info_patient_use,by="ID")


###########################################################################################
## 保存文件

out_name <- paste0( out_path , "/Combine_CIN_sampleInfo.tsv" )
write.table( info_all , out_name , row.names = F , quote = F , sep = "\t" )